<html lang="zh-CN">

<head>
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>发表主题</title>
	<link rel="stylesheet" href="js/filedisk.css" type="text/css">
	<style type="text/css">
		.text-container {
			/* 只能显示一行文字的样式*/
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
		}

		.thin-table {
			border-collapse: collapse;
			/* 折叠边框 */
			width: 80%;
		}

		.thin-table {
			border-left: 1px solid #c3d0da;
			/* table的左边框 */
			border-right: 1px solid #c3d0da;
			/* table的右边框 */
			border-top: 1px solid #c3d0da;
			/* table的顶部边框 */
			border-bottom: 1px solid #c3d0da;
			/* table的底部边框 */
		}

		.thin-table th,
		.thin-table td {
			padding: 12px;
			/* 单元格内边距 */
			text-align: left;
			/* 单元格内文本对齐方式 */
			vertical-align: top;
			/* 单元格内文本垂直对齐方式 */
			border-top: 1px solid #c3d0da;
			/* 仅显示下边框线 */
			border-bottom: 1px solid #c3d0da;
			/* 仅显示下边框线 */
		}

		.c1 {
			width: 100px;
			height: 100px;
			border: 3px solid #F3970C;
			border-radius: 50%;
			overflow: hidden;
		}

		label {
			position: relative;
		}

		#userimg {
			position: absolute;
			left: 1px;
			top: 0px;
			opacity: 0;
			width: 100px;
			height: 24px;
		}

		#userbtn {
			font-size: 16px;
			padding: 1px 1px;
			background: none;
			color: #0055ff;
			border: none;
			width: 100px;
		}

		.STYLE2 {
			color: #FF0000
		}
	</style>
</head>

<body>
	<table width="100%" height="100%" border="0" cellpadding="1" cellspacing="0" class="TitleColor">
		<tr>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td height="30px"><a href="forum.htm">首页 </a> » <a href="javascript:history.back();"> 返回 </a> » 发表主题</td>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td>&nbsp;</td><!--左边空白间隙部分-->

			<td width="60%" height="100%" valign="top">
				<table width="100%" height="100%" border="1" cellpadding="0" cellspacing="0"><!--中间显示部分-->
					<tr>
						<td width="100%" height="100%">
							<table width="100%" height="100%" border="0" bgcolor="#FFFFFF">
								<tr>
									<td>&nbsp;</td>
									<td width="95%" valign="top" height="100%">
										<table width="100%" border="0" cellpadding="1" cellspacing="0"><!--中间显示部分-->
											<tr>
												<td height="40px">标题</td>
											</tr>
											<tr>
												<td><input type="text" id='title' placeholder="输入标题内容"
														style="height:43px;width:100%;"></td>
											</tr>
											<tr>
												<td height="40px">内容</td>
											</tr>
											<tr>
												<td> <textarea
														style="min-height:500px; min-width:100%; max-height:80%; max-width:400px;"
														placeholder="输入内容" id="posttext" maxlength=''></textarea></td>
											</tr>
											<tr>
												<td>&nbsp;</td>
											</tr>


											<tr>
												<td>
													<label for="userimg">
														<input type="button" id="userbtn" value="插入图片" align="middle">
														<input type="file" id="userimg" onChange="selectImg(this);"
															accept="image/*" title="选择图片">
													</label>
												</td>
											</tr>
											<tr>
												<td id="bar" style="visibility: hidden;"><progress id="upload-bar"
														value="0" max="100"></td>
											</tr>


											<tr>
												<td>&nbsp;</td>
											</tr>
											<tr>
												<td align="left"><input type="submit" onClick="confirm()" value="发布"
														style="height:40px;width:100px;"></td>
											</tr>
											<tr>
												<td>&nbsp;</td>
											</tr>
											<tr>
												<td><span class="STYLE2">温馨提示: 帖子需审核后才会显示哦，后台会在发贴后8小时内审核。请耐心等待!</span>
												</td>
											</tr>
										</table>
									</td><!--中间显示部分-->

									<td>&nbsp;</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</td><!--中间显示部分-->


			<td>&nbsp;</td><!--右边空白间隙部分-->
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td height="50" align="center">
				<div id='footerpage'></div>
			</td>
			<td>&nbsp;</td>
		</tr>
	</table>

</body>
<!--
<script src="js/blueimp-md5-2.18.0.js"></script>
<script src="js/jquery-3.5.1.min.js"></script>
<script src="js/spark-md5.min.js"></script>
-->
<script>
	function loadScript(c, d) { var b = document.createElement("script"); b.type = "text/javascript"; var e = sessionStorage.getItem(c); e ? (b.textContent = e, d(b)) : fetch(c).then(function (a) { return a.text() }).then(function (a) { b.textContent = a; sessionStorage.setItem(c, a); d(b); location.reload() })["catch"](function (a) { console.error("Error loading the script:", a) }) };
	loadScript("tu_0.js", function (script) { document.head.appendChild(script); });
</script>
<script type="text/javascript" language="javascript">
	if (sessionStorage.getItem("tu_0.js")) {		// 读取记忆是否已保存js文件
		loadHtml("footer.htm", "footerpage");
	}

	var WorksType = sessionStorage.getItem("WorksType");
	var ForumId = sessionStorage.getItem("ForumId");
	var TitleText = sessionStorage.getItem("TitleText");
	document.getElementById("title").value = TitleText;
	//console.log('AA--'+WorksType+"--bb---"+ForumId);

	var first = 0;
	var tick = 0;
	var repeat = 0;
	var rethdst = "";
	function fun() {
		if (first == 0) {
			first = 1;
			setTimeout(function () { myTimer("cookie", 0, 0, 0, 0) }, 0);		//自动发送post指,只有打开页面的时候，才发送一次	
		} else {
			if (++tick > 50) {
				tick = 0;
				if (repeat == 1) {
					if (rethdst != null && rethdst != undefined) {
						myTimer("checkFinish", 0, 0, 0, 0);
					}
				}
			}
		}
	}
	setInterval("fun()", 1000);			// 周期性调用函数 每隔100毫秒调用一次

	function confirm() {
		var title = document.getElementById("title").value;
		var posttext = document.getElementById("posttext").value;
		if (title == "") {
			alert("标题不能为空");
			return false;
		}
		if (posttext == "") {
			alert('内容不能为空');
			return false;
		}
		myTimer("postopr", 0, 0, 0, 0);
	}

	function selectImg() {
		document.getElementById("bar").style.visibility = "visible";		// 显示被隐藏的内容
		var fileInput = document.getElementById('userimg');
		if (!fileInput.files || !fileInput.files[0]) {
			alert("无效文件");
			return;
		}
		var file = fileInput.files[0];
		calculate(file, 0, function (md5) {
			var HEAD = new Object;
			HEAD.OptTyp = makeType(0, 0, 0x0e); //操作类型 ROOM_USER USER_ACCOUNT KCB_LIST_USER_HEAD 用户头像类型
			HEAD.Kcid = kcidNum;
			HEAD.FileId = 0x102; 				// IMG_LARGE=0x102 头像规格 IMG_BG_HOST=0x103 作品主页背景图片 IMG_BG_CIRCLE=0x104 朋友圈背景 IMG_BG_CHAT=0x105 聊天背景
			HEAD.AuxId1 = 0; 					// 辅助扩展Id1 commentId[聊天时发文件 等]
			HEAD.AuxId2 = 0; 					// 辅助扩展Id2 replyId[聊天时发文件 等]			
			HEAD.FileMD5 = md5;					// 上传文件的MD5 16字符
			HEAD.FileLen = file.size; 			// 上传文件的长度
			HEAD.Aux = file.name;				// 可以直接给"",TxtKeyArray会分辨//max=70扩展字符串 optTyp为ROOM_FREE时 aux为生成路径[aa$bb$cc.bin],其它时候可带参数 ['cid1234$rid5678' commentId:1234 $ replyId:5678等等
			HEAD.MTime = file.lastModified; 	//文件的最后修改时间

			HEAD.BlockMax = 0;
			HEAD.Hdst = "";
			HEAD.Assd = "";
			myTimer("checkFile", file, md5, HEAD, 0);		// 获取到的MD5数值
		})
	}

	function calculate(file, index, callBack) {
		var fileReader = new FileReader(),
			blobSlice = File.prototype.mozSlice || File.prototype.webkitSlice || File.prototype.slice,
			chunkSize = 2097152,	// 每次读取2MB
			chunks = Math.ceil(file.size / chunkSize),		// 总文件分为几段数据
			currentChunk = 0,
			spark = new SparkMD5();

		fileReader.onload = function (e) {
			spark.appendBinary(e.target.result);
			currentChunk++;
			if (currentChunk < chunks) {
				loadNext();
			} else {
				callBack(spark.end(), index);			// 最终获取到的MD5数值
			}
		};

		function loadNext() {
			var start = currentChunk * chunkSize,											// 数据开始位置
				end = start + chunkSize >= file.size ? file.size : start + chunkSize;		// 数据结束位置
			fileReader.readAsBinaryString(blobSlice.call(file, start, end));
		};
		loadNext();
	}

	var kcidNum = 0;
	function uploadfile(start, bhst, block, value, HEAD, BLOCK) {		// 上传文件	
		var progressBar = document.getElementById('upload-bar');
		xhr = new XMLHttpRequest();
		xhr.upload.addEventListener('progress', function (event) {
			if (event.lengthComputable) {
				var percentage = Math.round((event.loaded * 100) / event.total);
				//console.log('百分比BB' + percentage);
				progressBar.value = percentage;
			} else {
				console.log('百分比AA');
			}
		}, false);

		blobSlice = File.prototype.mozSlice || File.prototype.webkitSlice || File.prototype.slice
		xhr.onreadystatechange = function () {
			if (xhr.readyState === 4) {
				if (xhr.status === 200) {
					progressBar.value = 0;
					//console.log('成功');
					//myTimer("Cread", 0, 0, 0, 0);
					//console.log('AA上传文件块' + BLOCK.FileIdx + ':成功');
					document.getElementById("bar").style.visibility = "hidden";		// 显示被隐藏的内容


					var posttext = document.getElementById("posttext").value;
					document.getElementById("posttext").value = posttext + "bc";
				} else {
					progressBar.value = 0;
					alert('更换头像失败' + xhr.status);
					//alert('上传文件块' + BLOCK.FileIdx + ':失败 ' + xhr.status);
				}
			}
		};

		/*
		常用状态码（status）及其含义：
		404 没找到页面(not found)
		403 禁止访问(forbidden)
		500 内部服务器出错(internal service error)
		200 一切正常(ok)
		304 没有被修改(not modified)(服务器返回304状态，表示源文件没有被修改 )
		*/
		xhr.open('POST', 'upload.php?t:sbui&bhst:' + bhst, true);	//true-异步请求，false-同步请求
		xhr.send(blobSlice.call(block, start, block.size));		// start是开始的字节  file.size是结束的字节	   
		repeat = 1;
	}

	function makeType(roomTyp, subTyp, dbsTyp) {
		return ((roomTyp & 0x03) << 10) | ((subTyp & 0x1f) << 5) | (dbsTyp & 0x1f);
	}

	function myTimer(type, file, value, HEAD, BLOCK) {
		//function myTimer(type, file, value) {
		var phpName;
		if (type == "checkFile" || type == "checkStep" || type == "checkFinish") {
			//phpName = "uploadset.php";
			phpName = "filelist.php";
		} else if (type == "read" || type == "postopr") {
			phpName = "forum.php";
		} else {
			phpName = "index.php";
		}
		var xhr = postPhp(phpName);

		xhr.onreadystatechange = function () {			//2.注册回调函数
			var array_name = txtUnzip(xhr);
			if (array_name != null) {
				if (type == "cookie") {
					cookiemyFunction(array_name)	// 返回的信息在html页面显
				}
				if (type == "read") {
					myFunction(array_name)			// 返回的信息在html页面显
				}
				if (type == "postopr") {
					postoprmyFunction(array_name)	// 返回的信息在html页面显
				}

				if (type == "checkFile") {
					checkFileFunction(array_name, file, value, HEAD)	// 返回的信息在html页面显
				}
				if (type == "checkStep") {
					checkStepFunction(array_name, file, value, HEAD, BLOCK)	// 返回的信息在html页面显
				}
				if (type == "waiting") {
					waitFunction(array_name, file, HEAD, BLOCK)		// 返回的信息在html页面显
				}
				if (type == "checkFinish") {
					checkFinishFunction(array_name)	// 返回的信息在html页面显
				}

			}
		}

		if (type == "cookie") {
			txtZip(xhr, "t:ncki");
		}
		if (type == "postopr") {	// 添加标题
			var title = txtB64Enc(document.getElementById("title").value); // 标题
			var post = txtB64Enc(document.getElementById("posttext").value);// 内容
			var operate = 0; 	// 0增1删2改
			//console.log('CC--'+WorksType+"--DD--"+ForumId);
			txtZip(xhr, "t:sfpo&fidr:" + ForumId + "&fbwt:" + WorksType + "&fptt:" + title + "&fpdt:" + post + "&fopr:" + operate, 1);

			/*
			var title = ""; 	// 标题
			var post = "";		// 内容
			var operate = 0; 	// 0增1删2改
			switch (operate) {
				case 0: txtZip(xhr, "t:sfpo&fidr:" + ForumId + "&fbwt:" + WorksType + "&fptt:" + title + "&fpdt:" + post + "&fopr:" + operate); break; 
				case 1: txtZip(xhr, "t:sfpo&fidr:" + ForumId + "&fbwt:" + WorksType + "&fpid:" + PostId + "&fopr:" + operate); break;
				case 2: txtZip(xhr, "t:sfpo&fidr:" + ForumId + "&fbwt:" + WorksType + "&fpid:" + PostId + "&fptt:" + title + "&fpdt:" + post + "&fopr:" + operate); break; 
			}
			*/
		}


		if (type == "checkFile") {
			repeat = 0;
			var aux = txtB64Enc(HEAD.Aux);
			txtZip(xhr, "t:upcf&optt:" + HEAD.OptTyp + "&kcid:" + HEAD.Kcid + "&fida:" + HEAD.FileId + "&aux1:" + HEAD.AuxId1 + "&aux2:" + HEAD.AuxId2 +
				"&flen:" + HEAD.FileLen + "&mtim:" + HEAD.MTime + "&md5a:" + HEAD.FileMD5 + "&auxa:" + aux + "&dird:" + 0);
		}
		if (type == "checkStep") {
			var qty = BLOCK.qty;
			var md5s = "";
			for (var index = 0; index < qty; index++) {
				md5s += BLOCK.BlockMD5[index];
			}
			txtZip(xhr, "t:upcs&flen:" + HEAD.FileLen + "&md5a:" + HEAD.FileMD5 + "&bmax:" + HEAD.BlockMax + "&hdst:" + HEAD.Hdst + "&md5s:" + md5s);
			// console.log("发送指令 t:upcs&flen");
		}
		if (type == "waiting") {
			var index = BLOCK.Using;
			var aux = txtB64Enc(HEAD.Aux);
			txtZip(xhr, "t:upwt&optt:" + HEAD.OptTyp + "&kcid:" + HEAD.Kcid + "&fida:" + HEAD.FileId + "&aux1:" + HEAD.AuxId1 + "&aux2:" + HEAD.AuxId2 + "&mtim:" + HEAD.MTime + "&auxa:" + aux
				+ "&md5a:" + HEAD.FileMD5 + "&flen:" + HEAD.FileLen
				+ "&bmd5:" + BLOCK.BlockMD5[index] + "&bsct:" + BLOCK.BlockSector[index] + "&blen:" + BLOCK.BlockLen[index]
				+ "&didx:" + BLOCK.DiskIdx[index] + "&fidx:" + BLOCK.FileIdx[index]);
		}
		if (type == "checkFinish") {
			txtZip(xhr, "t:upch&hdst:" + rethdst);
		}


	}

	function checkFileFunction(arr, file, value, HEAD) {
		// result,bmax,fida,hdst,assd, 
		switch (arr.result) {
		case "ok":{

			HEAD.BlockMax = arr.bmax;
			HEAD.FileId = arr.fida;
			HEAD.Hdst = arr.hdst;
			// HEAD.Assd = arr.assd;
			rethdst = arr.hdst;
			console.log(" checkFile Function file:" + file.name + " HEAD BlockMax:" + HEAD.BlockMax + " Hdst:" + HEAD.Hdst+ " Assd:" + HEAD.Assd);
			// 开始分文件块 
			// arr.bloc = 每个文件块的长度 一般为16M
			// arr.hdst = 文件头结构
			// arr.assd = [{0,0,0},{1,0,288}] 每个文件块分配的 FileIdx SlaveIdx Slave写入地址

			// 将要发的文件分段 每个文件块 16M 
			var BLOCK = new Object;
			// var list = JSON.parse(HEAD.Assd);
			BLOCK.qty = Math.ceil(file.size / HEAD.BlockMax); // 服务器返回 文件块的数量
			BLOCK.FileMD5 = HEAD.FileMD5;
			BLOCK.FileLen = HEAD.FileLen;
			BLOCK.Using = 0;

			BLOCK.BlockMD5 = new Array();
			BLOCK.BlockSector = new Array();
			BLOCK.BlockLen = new Array();
			BLOCK.FileIdx = new Array();
			BLOCK.DiskIdx = new Array();
			BLOCK.ErrNum = new Array();
			BLOCK.Bhst = new Array();

			blobSlice = File.prototype.mozSlice || File.prototype.webkitSlice || File.prototype.slice;
			for (var index = 0, times = 0; index < BLOCK.qty; index++) { // blockMax = 16M 文件块长度
				var left = (file.size - index * HEAD.BlockMax);
				BLOCK.BlockLen[index] = left >= HEAD.BlockMax ? HEAD.BlockMax : left; 			// 文件块长度
				BLOCK.ErrNum[index] = 0;
				BLOCK.Bhst[index] = "";

				var offset = index * HEAD.BlockMax;
				var end = offset + BLOCK.BlockLen[index];
				var blob = blobSlice.call(file, offset, end);
				calculate(blob, index, function (md5, idx) {
					BLOCK.BlockMD5[idx] = md5;
					if (++times >= BLOCK.qty) {
						// alert("BLOCKAAAAAAA " + idx + "BlockMD5:" + BLOCK.BlockMD5[0] + "BlockSector:" + BLOCK.BlockSector[0]);
						myTimer("checkStep", file, value, HEAD, BLOCK);
					}
				});
			}
		} break;
		case "file":
			alert("上传失败");
			return false;
			break;
		case "file exist":
			repeat = 0;
			alert("checkFile 文件已存在");
			break;
		default:
			alert("checkFile其它错误:" + arr.result);
			return false;
		}
	}
	function checkStepFunction(arr, file, value, HEAD, BLOCK) {
		switch (arr.result) {
		case "ok":{
			var list = JSON.parse(arr.assd);
			// alert("checkStep Function blockMax:" + arr.bmax +" hdst:" + arr.hdst + " AssignDisk:" + arr.assd + "list.length:" + list.length + "BLOCK.qty:" + BLOCK.qty);
			for (var index = 0; index < BLOCK.qty; index++) { // blockMax = 16M 文件块长度
				BLOCK.DiskIdx[index] = 0;
				BLOCK.BlockSector[index] = 0;
				for (var idx2 = 0; idx2 < list.length; idx2++) {
					if (list[idx2][0] == index) {
						BLOCK.FileIdx[index] = index;
						BLOCK.DiskIdx[index] = list[idx2][1];
						BLOCK.BlockSector[index] = list[idx2][2];
						BLOCK.ErrNum[index] = list[idx2][3];
					}
				}
				if (BLOCK.ErrNum[index] != 6 && BLOCK.ErrNum[index] != 0) { // 6=FILE_EXIST 文件存在
					BLOCK.Using = index;
					myTimer("waiting", file, BLOCK.BlockMD5[index], HEAD, BLOCK);
				}
			}
		}break;
		case "file":
			alert("checkStep上传失败");
			return false;
			break;
		case "file exist":
			repeat = 0;
			alert("checkStep 文件已存在");
			break;
		default:
			alert("checkStep其它错误:" + arr.result);
			return false;
		}
	}
	function waitFunction(arr, block, HEAD, BLOCK) {
		// alert("waitFunction+ "+arr.result);
		switch (arr.result) {
		case "ok":
			BLOCK.Bhst = arr.bhst;
			uploadfile(arr.offs, arr.bhst, block, HEAD, BLOCK);
			break;
		case "file":
			alert("wait上 传失败");
			return false;
			break;
			default:
				alert("wait 其它错误:" + arr.result);
				return false;
				break;
		}
	}
	function checkFinishFunction(arr) {
		switch (arr.result) {
		case "ok":{
			// "result":"ok",list:[[0,0,0,6][0,0,0,6]]
			isComplete = 0;
			var list = JSON.parse(arr.assd);
			isArrayComplete(list, 0); // 二维数组返回结果 处理
			if (isComplete == 2) { // 全成功
				repeat = 0;
				rethdst = null; // 完成后
				alert("checkFinish上传 成功");
				document.getElementById("bar").style.visibility = "hidden";		// 显示被隐藏的内容
			}
		}break;
		case "file exist":
			repeat = 0;
			rethdst = null; // 完成后
			alert("checkFinish上传 成功");
			myTimer("getimg", 0, 257, 0, 0);
		break;
		case "file":
			alert("checkFinish上传失败");
			return false;
			break;
		case "file in temp":
			alert("checkFinish 文件在临时目录");
			return false;
			break;
		case "file unexist":
			alert("checkFinish 文件不存在");
			return false;
			break;
		default:
			alert("checkFinish 其它错误:" + arr.result);
			return false;
		}
	}

	function cookiemyFunction(arr) {
		switch (arr.result) {
			case "ok":
				//myTimer("read",0,0);
				break;
			default:
				alert('请先登录后，再操作！');
				window.location.href = "user.htm";
				return false;
		}
	}
	function postoprmyFunction(arr) {
		switch (arr.result) {
			case "ok":
				window.location.href = "forum.htm";
				break;
			default:
				alert('添加失败，请重新添加');
				return false;
		}
	}

</script>

</html>